package com.starblink.feedback.ui

import android.content.Context
import android.view.View
import android.widget.TextView
import com.apollographql.apollo3.api.Optional
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.toast
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.android.basic.user.LoginCheckInterceptor
import com.starblink.android.basic.widget.dialog.BaseBottomSheetFragment
import com.starblink.android.basic.widget.flowlayout.FlowLayout
import com.starblink.android.basic.widget.flowlayout.TagAdapter
import com.starblink.basic.util.CommUtils
import com.starblink.feedback.R
import com.starblink.feedback.databinding.ReportDialogLayoutBinding
import com.starblink.rocketreserver.Report4ContentMutation
import com.starblink.rocketreserver.type.PostReportRequest
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch


class ReportPostDialog(
    val contxt: Context,
    val targetId: String = "0",
    val reportType: Int,
    val content: String = "",
    val picUrl: String = "",
    val videoUrl: String = "",
) : BaseBottomSheetFragment() {

    var selectReason = ""
    private val reportList = listOf(
        "spam",
        "Pornographic content",
        "Induced self-harm",
        "Harassment or invasion of privacy",
        "Violent scenes",
        "infringement",
        "Dangerous goods",
        "hatred",
        "wrong message",
    )
    lateinit var binding: ReportDialogLayoutBinding
    override fun getBindingRootView(): View {
        binding = ReportDialogLayoutBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initView() {
        binding.close.click {
            dismiss()
        }

        binding.flTopic.adapter = object : TagAdapter<String?>(reportList) {
            override fun getView(parent: FlowLayout?, position: Int, s: String?): View {
                val tv: TextView = View.inflate(
                    contxt,
                    R.layout.item_report,
                    null
                ) as TextView
                tv.text = s
                if (selectReason == s) {
                    tv.isSelected = true
                    tv.setTextColor(CommUtils.getColor(com.starblink.basic.style.R.color.sk_red600))
                    tv.setBackgroundResource(R.drawable.bg_red4_radius_corner_btn)
                } else {
                    tv.isSelected = false
                    tv.setBackgroundResource(R.drawable.bg_gray3_radius_corner_btn)
                    tv.setTextColor(CommUtils.getColor(com.starblink.basic.style.R.color.sk_gray800))
                }
                return tv
            }
        }
        binding.flTopic.setOnTagClickListener { view, position, parent ->
            selectReason = reportList[position]
            binding.flTopic.adapter.notifyDataChanged()
            true
        }
//        binding.flTopic.setOnSelectListener(OnSelectListener {
//            selectReason = it.toString()
//            binding.flTopic.adapter.notifyDataChanged()
//        })

        binding.btnSure.click {//确认举报
            LoginCheckInterceptor.check2Login(contxt) {
                CoroutineScope(Dispatchers.IO).launch {
                    val response = SkGqlClient.skMutation(
                        Report4ContentMutation(
                            PostReportRequest(
                                targetId = targetId,
                                reportType = reportType,
                                reportReason = Optional.present(selectReason),
                                reasonType = selectReason,
                                videoLink = Optional.present(videoUrl),
                                postPic = Optional.present(picUrl),
                                contentText = Optional.present(content),
                            )
                        ),
                        showDefaultLoading = true
                    )
                    if (response?.data != null && response.data!!.report4Content != null && response.data!!.report4Content) {
                        MainScope().launch {
                            toast("Reported successfully")
                            dismiss()
                        }
                    }
                }
            }
        }
    }

}